package com.cindata

import com.cindata.dictionary.*

class Collateral
{

    Province province
    // 省份
    City city
    // 城市
    District district
    // 区县
    EstateStatusDict estateStatus
    // 押品状态
    Boolean isEstate = true
    // 是否是押品
    BigDecimal unitPrice
    // 评估单价
    BigDecimal totalPrice
    // 评估总价
    Date valuationDate
    // 评估时间
    BigDecimal ltv
    // 抵押率 （%）
    BigDecimal cltv
    // 动态抵押率 （%）
    Account account
    // 直属管理机构(银行网点)
    // Mortgage mortgage     			   // 贷款
    // MortgageStatusDict mortgageStatus  // 贷款状态

    String externalId
    // 质押物编号
    String name
    // 质押物名称
    String fullName
    // 出质人名称
    ContactIdentityType identityType
    // 出质人证件类型
    String idNumber
    // 出质人证件号码
    String loanCardNumber
    // 出质人贷款卡编号
    String commonOwnerFullName
    // 其他共同所有人
    Double sharePoint
    // 所占份额

    Cashability cashability
    // 变现能力 1.通用且变现容易，2.通用但变现较不易，专用性强
    String valueVolatility
    // 价值波动性 1.价值波动大；2.价值波动中；3.有升值潜力或波动小
    String seizureOfConvenience
    // 查封便利性 1.无查封障碍的不动产，动产由我行监管；2.查封有一定障碍的不动产，动产存放地点相对固定但非我行监管；3.查封有障碍的不动产，动产存放地点难掌握和控制
    String relationOfMortgagor
    // 抵质押物与借款人相关性 1.本人 2.第三方
    String legalValidity
    // 法律有效性 1.担保合法且手续完整 2.担保不合法或手续基本无效
    Double firstEvaluatePrice
    // 初次评估价值
    Account firstEvaluateAccount
    // 初次评估机构
    Double latestEvaluatePrice
    // 最新评估价值
    Account latestEvaluateAccount
    // 最新评估机构
    Date latestEvaluateDate
    // 最新评估日期
    Date nextEvaluateDate
    // 下次评估日期
    Double actualMortgageRate
    // 实际抵质押率 (实际抵质押率=债权本息总额÷抵押物评估价值（或质押物认定价值）×100%)
    Double mortgageRateUpperLimit
    // 抵质押率上限
    String description
    // 抵质押物说明/权利简要说明
    String registerFullName
    // 登记人
    String registerAccount
    // 登记机构
    Date registerDate
    // 登记日期
    Date registerUpdateDate
    // 更新日期

    Date startDate
    // 押品抵押开始日期
    Date endDate
    // 押品抵押截指日期
    
    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User user
    User createBy
    User modifyBy

    Estate estate
    Vehicle vehicle
    FinancialPledge financialPledge
    AccountsReceivable accountsReceivable
    OtherCollateral otherCollateral

    CollateralType type

    Integer sourceId
    // 源对象ID
    String sourceClass
    // 源对象ClassName

    String status = "Pending"
    Boolean isInWorkflow = false

    //失败原因
    String descriptionOfFailure
    //失败原因说明

    Collateral replacementCollateral
    // 置换押品

    static hasMany = [comments: Comment, warrants: Warrant, contacts: CollateralContact, attachments: Attachment]

    static mapping = {
        autowire true
    }

    static constraints = {

        estate nullable: true, blank: true, comment: '房产'
        estate unique: true
        vehicle nullable: true, blank: true, comment: '车辆'
        financialPledge nullable: true, blank: true, comment: '金融质押品'
        accountsReceivable nullable: true, blank: true, comment: '应收账款'
        otherCollateral nullable: true, blank: true, comment: '其他抵质押品'

        province nullable: true, blank: true, comment: '省份'
        city nullable: true, blank: true, comment: '城市'
        district nullable: true, blank: true, comment: '区县'
        estateStatus nullable: true, blank: true, comment: '贷款状态'
        isEstate nullable: true, blank: true, comment: '是否在押'
        unitPrice nullable: true, blank: true, comment: '评估单价'
        totalPrice nullable: true, blank: true, comment: '评估总价'
        valuationDate nullable: true, blank: true, comment: '评估时间'
        ltv nullable: true, blank: true, comment: '抵押率 （%）'
        cltv nullable: true, blank: true, comment: '动态抵押率 （%）'
        account nullable: true, blank: true, comment: '属管理机构(银行网点)'

        externalId maxSize: 50, nullable: true, blank: true, comment: '质押物编号'
        name maxSize: 50, nullable: true, blank: true, comment: '质押物名称'
        fullName maxSize: 20, nullable: true, blank: true, comment: '出质人名称'
        identityType nullable: true, blank: true, comment: '出质人证件类型'
        idNumber maxSize: 18, nullable: true, blank: true, comment: '出质人证件号码'
        loanCardNumber maxSize: 50, nullable: true, blank: true, comment: '出质人贷款卡编号'
        commonOwnerFullName maxSize: 20, nullable: true, blank: true, comment: '其他共同所有人'
        sharePoint nullable: true, blank: true, comment: '所占份额'

        cashability nullable: true, blank: true, comment: '变现能力'
        valueVolatility maxSize: 50, nullable: true, blank: true, comment: '价值波动性'
        seizureOfConvenience maxSize: 50, nullable: true, blank: true, comment: '查封便利性'
        relationOfMortgagor maxSize: 50, nullable: true, blank: true, comment: '抵质押物与借款人相关性'
        legalValidity maxSize: 50, nullable: true, blank: true, comment: '法律有效性'
        
        firstEvaluatePrice nullable: true, blank: true, comment: '初次评估价值'
        firstEvaluateAccount nullable: true, blank: true, comment: '初次评估机构'
        latestEvaluatePrice nullable: true, blank: true, comment: '最新评估价值'
        latestEvaluateAccount nullable: true, blank: true, comment: '最新评估机构'
        latestEvaluateDate nullable: true, blank: true, comment: '最新评估日期'
        nextEvaluateDate nullable: true, blank: true, comment: '下次评估日期'
        actualMortgageRate nullable: true, blank: true, comment: '实际抵质押率'
        mortgageRateUpperLimit nullable: true, blank: true, comment: '抵质押率上限'
        description maxSize: 512, nullable: true, blank: true, comment: '抵质押物说明'
        registerFullName maxSize: 255, nullable: true, blank: true, comment: '登记人'
        registerAccount maxSize: 50, nullable: true, blank: true, comment: '登记机构'
        registerDate nullable: true, blank: true, comment: '登记日期'
        registerUpdateDate nullable: true, blank: true, comment: '更新日期'

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        startDate nullable: true, blank: true
        endDate nullable: true, blank: true
        
        type nullable: true, blank: true

        sourceClass maxSize: 50, nullable: true, blank: true
        sourceId nullable: true, blank: true

        status maxSize: 20, nullable: true, blank: true
        status inList: ["Pending", "Failed", "Completed"]
        
        descriptionOfFailure maxSize: 200, nullable: true, blank: true

        replacementCollateral nullable: true, blank: true
        isInWorkflow nullable: true, blank: true
    }

    def springSecurityService
    def collateralService
    def beforeUpdate()
    {
        totalPrice = latestEvaluatePrice
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    def beforeInsert()
    {
        // def username = springSecurityService?.getPrincipal()?.username
        // createBy = User.findByUsername(username)
        // account = createBy?.account
    }
}
